<?php

require_once(LIB_PATH.'/libRVT.php');

$rvt = new RVT();

if (isset($_POST['action'])){
	$db = conecta();
	
	switch ($_POST['action']){
		case "add":
			if ( isset($_POST['items']) && isset($_POST['scripts']) ){
				$xml = new XMLReader();
				$xml->open(INC_PATH.'/scripts.xml');
				$info = xml2assoc($xml);
				$xml->close();
				$scnames = array();
				foreach ($info[0]['value'] as $sc){
					foreach ($sc['value'] as $p){
						if ($p['tag'] == 'id')
							$id=$p['value'];
						if ($p['tag'] == 'name')
							$name=$p['value'];
					}
					$scnames[$id]=$name;
				}
				
				// Para cada script e item un mysql insert
				$items = array();
				foreach (explode('+',$_POST['items']) as $item)
					$items = array_merge($items, $rvt->exploitDisks($item));
				$items = array_unique($items);
				$scripts = explode('+',substr($_POST['scripts'],0,-1));
				foreach ($items as $item){
					foreach ($scripts as $script){
						$db->query('INSERT INTO queue SET idUser="1", item="'.mysql_real_escape_string($item).'", script="'.mysql_real_escape_string($scnames[$script]).'", pid="0", proc="0", prio="10", status="0";');
					}
				}
				
				exit();
			}
			break;
		case "del":
			if (isset($_POST['id']))
				$db->query('DELETE FROM queue WHERE idProc="'.mysql_real_escape_string($_POST['id']).'";');
			break;
		case "prio":
			if (isset($_POST['id']) && isset($_POST['prio']))
				$db->query('UPDATE queue SET prio="'.mysql_real_escape_string($_POST['prio']).'" WHERE idProc="'.mysql_real_escape_string($_POST['id']).'";');
			break;
		case "show":
			$db->query('SELECT * FROM queue WHERE item LIKE "'.$_SESSION['case'].'%" ORDER BY prio DESC;');
			$procs=array();
			while ($row=$db->row()){
				if ($row['status'] == 0)
					$row['class'] = 'qWait';
				elseif ($row['status'] == 1)
					$row['class'] = 'qCur'; 
				elseif ($row['status'] == 2)
					$row['class'] = 'qDone'; 
				$procs[]=$row;
			}
//			print_r($procs);
//			exit();
			$smarty->assign('procs',$procs);
			$smarty->display($_POST['mod'].'.tpl');
			break;
	}
	
	$db->close();
}

?>
